﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FinancialManagement.Utils;

namespace FinancialManagement.Models.Repository
{
    public class RevenueRepository
    {
        /// <summary>
        /// 根据单位编号获取部门收入预算信息
        /// </summary>
        /// <param name="depId"></param>
        /// <returns></returns>
        internal IList<BudgetApplyTable> GetBudgetRevenueByDepID(string depId, int year)
        {
            using (var dc = new FinancialMGTDataContext())
            {
                var query = from t in dc.T_BudgetApplyTable
                            select t;
                if (depId == Utils.Util.DEPFINANCIAL)
                {
                    return (from t in query
                            where t.Annual == year
                            select new BudgetApplyTable
                            {
                                RevenueTypeName = t.RevenueTypeName,
                                RevenueTypeCode = t.RevenueTypeCode,
                                Notes = t.Notes,
                                Illustration = t.Illustration,
                                ChargeStandard = t.ChargeStandard,
                                ChargeScopeName = t.ChargeScopeName,
                                ChargeScopeCode = t.ChargeScopeCode,
                                ChargeProjectName = t.ChargeProjectName,
                                CalculateUnitName = t.CalculateUnitName,
                                CalculateUnitCode = t.CalculateUnitCode,
                                BudgetTotalRreven = t.BudgetTotalRrevenue,
                                BudgetAccding = t.BudgetAccding,
                                ApplyID = t.ApplyID,
                                ApplyDepartmentName = t.ApplyDpartmentName,
                                ApplyDepartment = t.ApplyDpartmentID,
                                Annual = t.Annual,
                                ActualTotalRreven = t.ActualTotalRrevenue ?? 0
                            }).ToList();
                }
                else
                {
                    return (from t in query
                            where t.ApplyDpartmentID != null && t.ApplyDpartmentID.Equals(depId) && t.Annual == year
                            select new BudgetApplyTable
                            {
                                RevenueTypeName = t.RevenueTypeName,
                                RevenueTypeCode = t.RevenueTypeCode,
                                Notes = t.Notes,
                                Illustration = t.Illustration,
                                ChargeStandard = t.ChargeStandard,
                                ChargeScopeName = t.ChargeScopeName,
                                ChargeScopeCode = t.ChargeScopeCode,
                                ChargeProjectName = t.ChargeProjectName,
                                CalculateUnitName = t.CalculateUnitName,
                                CalculateUnitCode = t.CalculateUnitCode,
                                BudgetTotalRreven = t.BudgetTotalRrevenue,
                                BudgetAccding = t.BudgetAccding,
                                ApplyID = t.ApplyID,
                                ApplyDepartmentName = t.ApplyDpartmentName,
                                ApplyDepartment = t.ApplyDpartmentID,
                                Annual = t.Annual,
                                ActualTotalRreven = t.ActualTotalRrevenue ?? 0
                            }).ToList();
                }

            }
        }

        /// <summary>
        /// 根据流水号获取对应的明细
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        internal BudgetApplyTable GetBudgetRevenueByApplyId(int applyId)
        {
            using (var dc = new FinancialMGTDataContext())
            {
                var tModel = dc.T_BudgetApplyTable.FirstOrDefault(o => o.ApplyID == applyId);
                if (tModel != null)
                {
                    return new BudgetApplyTable
                    {
                        ApplyID = tModel.ApplyID,
                        ActualTotalRreven = tModel.ActualTotalRrevenue ?? 0,
                        Annual = tModel.Annual,
                        ApplyDepartment = tModel.ApplyDpartmentID,
                        ApplyDepartmentName = tModel.ApplyDpartmentName,
                        BudgetAccding = tModel.BudgetAccding,
                        BudgetTotalRreven = tModel.BudgetTotalRrevenue,
                        CalculateUnitCode = tModel.CalculateUnitCode,
                        CalculateUnitName = tModel.CalculateUnitName,
                        ChargeProjectName = tModel.ChargeProjectName,
                        ChargeScopeCode = tModel.ChargeScopeCode,
                        ChargeScopeName = tModel.ChargeScopeName,
                        ChargeStandard = tModel.ChargeStandard,
                        Illustration = tModel.Illustration,
                        Notes = tModel.Notes,
                        RevenueTypeCode = tModel.RevenueTypeCode,
                        RevenueTypeName = tModel.RevenueTypeName
                    };
                }
                else
                {
                    return new BudgetApplyTable();
                }
            }
        }

        /// <summary>
        /// 插入收入预算明细
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        internal bool InsertBudgetApplyTable(BudgetApplyTable model)
        {
            bool isOk = true;
            try
            {
                using (var dc = new FinancialMGTDataContext())
                {
                    var tModel = new T_BudgetApplyTable();
                    if (tModel != null)
                    {
                        tModel.ApplyID = Util.GetSerialID(Util.BUDGETAPPLYTABLE);
                        tModel.ActualTotalRrevenue = model.ActualTotalRreven;
                        tModel.Annual = model.Annual;
                        tModel.ApplyDpartmentID = model.ApplyDepartment;
                        tModel.ApplyDpartmentName = model.ApplyDepartmentName;
                        tModel.BudgetAccding = model.BudgetAccding;
                        tModel.BudgetTotalRrevenue = model.BudgetTotalRreven;
                        tModel.CalculateUnitCode = model.CalculateUnitCode;
                        tModel.CalculateUnitName = model.CalculateUnitName;
                        tModel.ChargeProjectName = model.ChargeProjectName;
                        tModel.ChargeScopeCode = model.ChargeScopeCode;
                        tModel.ChargeScopeName = model.ChargeScopeName;
                        tModel.ChargeStandard = model.ChargeStandard;
                        tModel.Illustration = model.Illustration;
                        tModel.Notes = model.Notes;
                        tModel.RevenueTypeCode = model.RevenueTypeCode;
                        tModel.RevenueTypeName = model.RevenueTypeName;

                        dc.T_BudgetApplyTable.InsertOnSubmit(tModel);
                        dc.SubmitChanges();
                    }
                    else
                    {
                        isOk = false;
                    }
                }
            }
            catch (Exception)
            {
                isOk = false;
            }

            return isOk;
        }

        /// <summary>
        /// 更新收入预算明细表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        internal bool UpdateBudgetApplyTable(BudgetApplyTable model)
        {
            bool isOk = true;
            try
            {
                using (var dc = new FinancialMGTDataContext())
                {
                    var tModel = dc.T_BudgetApplyTable.FirstOrDefault(o => o.ApplyID == model.ApplyID);
                    if (tModel != null)
                    {
                        tModel.ActualTotalRrevenue = model.ActualTotalRreven;
                        tModel.Annual = model.Annual;
                        tModel.ApplyDpartmentID = model.ApplyDepartment;
                        tModel.ApplyDpartmentName = model.ApplyDepartmentName;
                        tModel.BudgetAccding = model.BudgetAccding;
                        tModel.BudgetTotalRrevenue = model.BudgetTotalRreven;
                        tModel.CalculateUnitCode = model.CalculateUnitCode;
                        tModel.CalculateUnitName = model.CalculateUnitName;
                        tModel.ChargeProjectName = model.ChargeProjectName;
                        tModel.ChargeScopeCode = model.ChargeScopeCode;
                        tModel.ChargeScopeName = model.ChargeScopeName;
                        tModel.ChargeStandard = model.ChargeStandard;
                        tModel.Illustration = model.Illustration;
                        tModel.Notes = model.Notes;
                        tModel.RevenueTypeCode = model.RevenueTypeCode;
                        tModel.RevenueTypeName = model.RevenueTypeName;
                        dc.SubmitChanges();
                    }
                    else
                    {
                        isOk = false;
                    }
                }
            }
            catch (Exception)
            {
                isOk = false;
            }

            return isOk;
        }

        /// <summary>
        /// 删除收入预算明细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        internal bool DeleteBudgetApplyTableByApplyId(int applyId)
        {
            bool isOk = true;
            try
            {
                using (var dc = new FinancialMGTDataContext())
                {
                    var tModel = dc.T_BudgetApplyTable.FirstOrDefault(o => o.ApplyID == applyId);
                    if (tModel != null)
                    {
                        dc.T_BudgetApplyTable.DeleteOnSubmit(tModel);
                        dc.SubmitChanges();
                    }
                    else
                    {
                        isOk = false;
                    }
                }
            }
            catch (Exception)
            {
                isOk = false;
            }

            return isOk;
        }
    }
}